<?php
defined('IN_TS') or die('Access Denied.');
switch($ts){

	//确申报取
	case "attend":

		$js = intval($_GET['js']);

    	$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$planid = intval($_POST['planid']);
		$memberid = intval($_POST['memberid']);
		$strPlan = $new['plan']->find('plan',array(
			'planid'=>$planid,
		));

		if($userid<>$strPlan['userid']&&$GLOBALS['TS_USER']['isadmin']==0){
			getJson('您没有操作权限！',1);
		}

		$userNum = $new['plan']->findCount('plan_users',array(
			'planid'=>$planid,
			'userid'=>$memberid,
		));

		if($userNum==0){
			getJson('此人不在申报名单中',1);
		}


		//标记他完成了
		$new['plan']->update('plan_users',array(
			'planid'=>$planid,
			'userid'=>$memberid,
		),array(
			'status'=>1,
		));

		// 对积分进行处理
		//aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts'],$memberid);

		/* 对服务时长进行处理
		$strPlan = $new['plan']->find('plan',array(
			'planid'=>$planid,
		));
		$strType = $new['plan']->find('plan_type',array(
			'typeid'=>$strPlan['typeid'],
		));
		if($strType['isduty'] == 1){
			$term = intval((strtotime($strPlan['endtime'])-strtotime($strPlan['starttime']))/(1000*3600));
			$arrUser=$new['user']->find('user_info',array('userid'=>$memberid));
			$count_duty=$arrUser['count_duty']*1+$term*1;
			$new['user']->update('user_info',array(
				'userid'=>$memberid,
			),array(
				'count_duty'=>$count_duty,
			));
			aac('user') -> doScore('user', 'duty', 'verify',$memberid);
		}else{


		}
*/

		//统计一下达成的
		$sql = "select sum(quota) as sums from ts_plan_users where planid=".$planid.' and status=1';
		$arr=$db->once_fetch_assoc($sql);
		$userDoNum=$arr['sums'];

		$new['plan']->update('plan',array(
			'planid'=>$planid,
		),array(
			'count_userdo'=>$userDoNum
		));


		//plan
		$strPlan = $new['plan']->find('plan',array(
			'planid'=>$planid,
		));

		echo '1';

		break;

	//确认没领取
	case "absent":

		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$planid = intval($_POST['planid']);
		$memberid = intval($_POST['memberid']);
		$strPlan = $new['plan']->find('plan',array(
			'planid'=>$planid,
		));

		if($userid<>$strPlan['userid']&&$GLOBALS['TS_USER']['isadmin']==0){
			getJson('您没有操作权限！',1);
		}

		$userNum = $new['plan']->findCount('plan_users',array(
			'planid'=>$planid,
			'userid'=>$memberid,
		));

		if($userNum==0){
			getJson('此人不在申领名单中',1);
		}


		//标记他没取
		$new['plan']->update('plan_users',array(
			'planid'=>$planid,
			'userid'=>$memberid,
		),array(
			'status'=>0,
		));

		// 对积分进行处理
		//aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts'],$memberid);

		/* 对服务时长进行处理
		$strPlan = $new['plan']->find('plan',array(
			'planid'=>$planid,
		));
		$strType = $new['plan']->find('plan_type',array(
			'typeid'=>$strPlan['typeid'],
		));
		if($strType['isduty'] == 1){
			$term = intval((strtotime($strPlan['endtime'])-strtotime($strPlan['starttime']))/(1000*3600));
			$arrUser=$new['user']->find('user_info',array('userid'=>$memberid));
			$count_duty=$arrUser['count_duty']*1+$term*1;
			$new['user']->update('user_info',array(
				'userid'=>$memberid,
			),array(
				'count_duty'=>$count_duty,
			));
			aac('user') -> doScore('user', 'duty', 'verify',$memberid);
		}else{


		}
*/


		//统计一下达成的
		$sql = "select sum(quota) as sums from ts_plan_users where planid=".$planid.' and status=1';
		$arr=$db->once_fetch_assoc($sql);
		$userDoNum=$arr['sums'];

		$new['plan']->update('plan',array(
			'planid'=>$planid,
		),array(
			'count_userdo'=>$userDoNum
		));


		//plan
		$strPlan = $new['plan']->find('plan',array(
			'planid'=>$planid,
		));

		echo '1';

		break;

	//取消申领方案
	case "cancel":

		$js = intval($_GET['js']);

		$userid = intval($TS_USER['userid']);
		
		if($userid == 0) {
			echo 0;exit;
		}
		
		$planid = intval($_POST['planid']);
		
		$new['plan']->delete('plan_users',array(
		
			'planid'=>$planid,
			'userid'=>$userid,
		
		));

		//统计一下申报的
		$sql = "select sum(quota) as sums from ts_plan_users where planid=".$planid;
		$arr=$db->once_fetch_assoc($sql);
		$userPlanNum=$arr['sums'];
		


		$new['plan']->update('plan',array(
			'planid'=>$planid,
		),array(
			'count_userwish'=>$userPlanNum,
		));
		
		echo '1';
		
		break;
		
	//申领方案
	case "join":
	
		$js = intval($_GET['js']);
		
	
		$userid = intval($TS_USER['userid']);
		
		if($userid == 0) {
			getJson('请登录后再申领方案',$js);
		}
		
		$planid = intval($_POST['planid']);

        $strPlan = $new['plan']->find('plan',array(
            'planid'=>$planid,
        ));

        if($strPlan==''){
            getJson('不存在该方案！',$js);
        }




		$username = trim($_POST['username']);
		$phone = trim($_POST['phone']);
		$quota = trim($_POST['quota']);
		$groupid = intval($_POST['groupid']);

		if($username=='' || $phone=='' || $quota==''|| $groupid==''){
			getJson('姓名、手机号、申报数、分馆不能为空',$js);
		}



		//删除之前的申领信息
		$new['plan']->delete('plan_users',array(
			'planid'=>$planid,
			'userid'=>$userid,
		));
		
		
		$new['plan']->create('plan_users',array(
			'planid'=>$planid,
			'userid'=>$userid,
			'quota'=>$quota,
			'groupid' => $groupid,
			'limittime'=>strtotime("+7 day"),
			'status'=>0,
			'addtime'=>time(),
		));

		//更新用户信息
		$new['plan']-> update('user_info',array(
			'userid'=>$userid,
		),array(
			'username'=>$username,
			'phone'=>$phone,
		));


		//统计一下申领的
		$sql = "select sum(quota) as sums from ts_plan_users where planid=".$planid;
		$arr=$db->once_fetch_assoc($sql);
		$userPlanNum=$arr['sums'];
		

		$new['plan']->update('plan',array(
			'planid'=>$planid,
		),array(
			'count_userwish'=>$userPlanNum
		));

		getJson('申报成功，请在您承诺的时间之前完成捐赠。',1,1);
		
		
		break;
		
}
